package pl.vanta.utils.gui.buttons;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;

public class VColorButton extends JButton implements ActionListener
{
	private static final long	serialVersionUID	= 1L;

	private int					border				= 10;
	private Color				color;

	public VColorButton(Color color)
	{
		setColor(color);
		setPreferredSize(new Dimension(60, 30));

		this.addActionListener(this);
	}

	public Color getColor()
	{
		return this.color;
	}

	public void setColor(Color color)
	{
		this.color = color;

		repaint();
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		int border = this.border;
		int border2 = 2 * border;

		g.setColor(this.color);
		g.fillRect(border, border, this.getWidth() - border2, this.getSize().height - border2);

		g.setColor(Color.BLACK);
		g.drawRect(this.border, this.border, this.getWidth() - border2, this.getHeight() - border2);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		Color color = JColorChooser.showDialog(null, "Select color", this.color);

		if (color != null && !color.equals(this.color))
		{
			this.color = color;

			repaint();
		}
	}
}